package org.zyj.io.example.case9;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.nio.charset.StandardCharsets;
import java.util.Random;

class ClientHandler extends SimpleChannelInboundHandler<CustomProtocol> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        String msg = "今天是个好日子" + new Random().nextInt();
        var content = msg.getBytes(StandardCharsets.UTF_8);
        var customProtocol = new CustomProtocol(content.length, content);
        System.out.println("向服务端发送消息: " + customProtocol);
        ctx.writeAndFlush(customProtocol);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, CustomProtocol msg) throws Exception {
        System.out.println("接收到服务端回复消息: " + msg);
    }
}
